// src/pages/task/TaskManage.vue
<template>
	<div class="task-manage">
		<el-card>
			<template #header>
				<div class="header-bar">
					<span>工序质检任务管理</span>
					<el-button type="primary" size="small" @click="openDialog('add')">新增任务</el-button>
				</div>
			</template>

			<el-table :data="taskList" border stripe>
				<el-table-column prop="id" label="任务编号" width="120" />
				<el-table-column prop="process" label="工序" width="150" />
				<el-table-column prop="standard" label="绑定标准" />
				<el-table-column prop="person" label="执行人" width="120" />
				<el-table-column prop="status" label="状态" width="100">
					<template #default="scope">
						<el-tag :type="statusType(scope.row.status)">{{ scope.row.status }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="result" label="结果" width="100" />
				<el-table-column prop="time" label="创建时间" width="160" />
				<el-table-column label="操作" width="180">
					<template #default="scope">
						<el-button size="small" @click="openDialog('edit', scope.row)">编辑</el-button>
						<el-button size="small" type="danger" @click="deleteRow(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>

		<el-dialog :title="dialogTitle" v-model="dialogVisible" width="600px">
			<el-form :model="form" label-width="100px">
				<el-form-item label="任务编号">
					<el-input v-model="form.id" />
				</el-form-item>
				<el-form-item label="工序">
					<el-input v-model="form.process" />
				</el-form-item>
				<el-form-item label="绑定标准">
					<el-input v-model="form.standard" />
				</el-form-item>
				<el-form-item label="执行人">
					<el-input v-model="form.person" />
				</el-form-item>
				<el-form-item label="状态">
					<el-select v-model="form.status">
						<el-option label="未开始" value="未开始" />
						<el-option label="进行中" value="进行中" />
						<el-option label="已完成" value="已完成" />
					</el-select>
				</el-form-item>
				<el-form-item label="结果">
					<el-input v-model="form.result" placeholder="合格 / 不合格" />
				</el-form-item>
				<el-form-item label="创建时间">
					<el-date-picker v-model="form.time" type="datetime" style="width: 100%" />
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="submitForm">保存</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import { ref } from 'vue'
import dayjs from 'dayjs'

export default {
	setup() {
		const taskList = ref([
			...Array.from({ length: 10 }).map((_, i) => ({
				id: `TASK00${i + 1}`,
				process: `工序 ${i + 1}`,
				standard: `质检标准 ${i + 1}`,
				person: ['张三', '李四', '王五'][i % 3],
				status: ['未开始', '进行中', '已完成'][i % 3],
				result: ['合格', '不合格', ''][i % 3],
				time: dayjs().subtract(i, 'day').format('YYYY-MM-DD HH:mm')
			}))
		])

		const dialogVisible = ref(false)
		const dialogTitle = ref('')
		const form = ref({})
		const currentIndex = ref(-1)

		const openDialog = (type, row) => {
			dialogTitle.value = type === 'add' ? '新增任务' : '编辑任务'
			dialogVisible.value = true
			if (type === 'edit') {
				form.value = { ...row }
				currentIndex.value = taskList.value.findIndex(t => t.id === row.id)
			} else {
				form.value = {
					id: '', process: '', standard: '', person: '', status: '未开始', result: '', time: ''
				}
				currentIndex.value = -1
			}
		}

		const submitForm = () => {
			if (currentIndex.value >= 0) {
				taskList.value[currentIndex.value] = { ...form.value, time: dayjs(form.value.time).format('YYYY-MM-DD HH:mm') }
			} else {
				taskList.value.push({ ...form.value, time: dayjs(form.value.time).format('YYYY-MM-DD HH:mm') })
			}
			dialogVisible.value = false
		}

		const deleteRow = (row) => {
			taskList.value = taskList.value.filter(t => t.id !== row.id)
		}

		const statusType = (status) => {
			return status === '已完成' ? 'success' : status === '进行中' ? 'warning' : 'info'
		}

		return {
			taskList,
			dialogVisible,
			dialogTitle,
			form,
			openDialog,
			submitForm,
			deleteRow,
			statusType
		}
	}
}
</script>

<style scoped>
.task-manage {
	padding: 20px;
}

.header-bar {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-weight: bold;
	font-size: 16px;
}
</style>
